package cn.xinfei.xdecision.data.core.frame.strategy;

import cn.xinfei.xdecision.common.model.datax.exception.XDecisionDataExceptionType;
import cn.xinfei.xdecision.common.service.rpc.RequestContextConstant;
import cn.xinfei.xdecision.data.model.IVariableRequest;
import cn.xinfei.xdecision.data.core.frame.check.impl.BaseVarCheckService;
import cn.xinfei.xdecision.data.core.util.VarCommonUtils;
import cn.xinfei.xdecision.data.core.util.XyfPreConditions;
import cn.xinfei.xdecision.data.core.vo.VarInterfaceInfoVO;
import cn.xinfei.xdecision.data.core.vo.standard.HttpStandardVarRequest;
import cn.xinfei.xdecision.data.core.vo.virtual.VirtualVarRequest;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

public abstract class BaseStrategy extends BaseVarCheckService implements VariableStrategy {

    public void checkStrategyInput(IVariableRequest request) {
        XyfPreConditions.checkArgument(
                CollectionUtils.isNotEmpty(
                        Optional.of(request)
                                .map(IVariableRequest::varCodes)
                                .orElse(null)) && !VarCommonUtils.isContainsEmptyStr(request.varCodes()),
                "varCodes 不能为空！"
        );

        XyfPreConditions.checkArgument(
                Optional.of(request)
                        .map(IVariableRequest::inputParams)
                        .map(JSONObject::size)
                        .orElse(0) > 0,
                "inputParams 不能为空！"
        );

        XyfPreConditions.checkArgument(
                StringUtils.isNotBlank(
                        Optional.of(request)
                                .map(IVariableRequest::requestId).orElse(null)),
                "requestId 不能为空！"
        );
    }

    public void checkVirtualInterfaceInput(VirtualVarRequest request) {
        XyfPreConditions.checkArgument(
                StringUtils.isNotBlank(
                        Optional.of(request)
                                .map(VirtualVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getMethod).orElse(null)) ||
                        StringUtils.isNotBlank(
                                Optional.of(request)
                                        .map(VirtualVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getUrl).orElse(null)) ||
                        StringUtils.isNotBlank(
                                Optional.of(request)
                                        .map(VirtualVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getRequestType).orElse(null)) ||
                        Objects.isNull(
                                Optional.of(request)
                                        .map(VirtualVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getIsCharge).orElse(null))
                ,
                XDecisionDataExceptionType.DATA_ACCESS_INTERFACE_SOURCE_REQUEST_MISSING_ERROR
        );

    }

    public void checkStandardInterfaceInput(HttpStandardVarRequest request) {
        XyfPreConditions.checkArgument(
                StringUtils.isNotBlank(
                        Optional.of(request)
                                .map(HttpStandardVarRequest::requestId).orElse(null)),
                "requestId 不能为空！"
        );
        XyfPreConditions.checkArgument(
                StringUtils.isNotBlank(
                        Optional.of(request)
                                .map(HttpStandardVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getMethod).orElse(null)) ||
                        StringUtils.isNotBlank(
                                Optional.of(request)
                                        .map(HttpStandardVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getRequestBody).orElse(null)) ||
                        StringUtils.isNotBlank(
                                Optional.of(request)
                                        .map(HttpStandardVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getUrl).orElse(null)) ||

                        StringUtils.isNotBlank(
                                Optional.of(request)
                                        .map(HttpStandardVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getRequestType).orElse(null)) ||
                        Objects.isNull(
                                Optional.of(request)
                                        .map(HttpStandardVarRequest::getVarInterfaceInfoVO).map(VarInterfaceInfoVO::getIsCharge).orElse(null))
                ,
                XDecisionDataExceptionType.DATA_ACCESS_INTERFACE_SOURCE_REQUEST_MISSING_ERROR
        );

    }


    protected void checkCreditInputParams(IVariableRequest variableApiRequest) {
        Optional<JSONObject> inputParams = Optional.of(variableApiRequest).map(IVariableRequest::inputParams);
        if (inputParams.isPresent()) {
            XyfPreConditions.checkArgument(
                    StringUtils.isNotBlank(inputParams.get().getString(RequestContextConstant.SEQ_ID)),
                    "征信必传参数 seq_id 不能为空！");

            XyfPreConditions.checkArgument(
                    StringUtils.isNotBlank(inputParams.get().getString(RequestContextConstant.BIZ_TYPE)),
                    "征信必传参数 biz_type 不能为空！");

            XyfPreConditions.checkArgument(
                    StringUtils.isNotBlank(inputParams.get().getString(RequestContextConstant.COLUMN_REQUESTER)),
                    "征信必传参数 requester 不能为空！");

            XyfPreConditions.checkArgument(
                    StringUtils.isNotBlank(inputParams.get().getString(RequestContextConstant.SOURCE_TYPE)),
                    "征信必传参数 source_type 不能为空！");

            XyfPreConditions.checkArgument(
                    StringUtils.isNotBlank(inputParams.get().getString(RequestContextConstant.BIZ_FLOW_NUMBER)),
                    "征信必传参数 biz_flow_number 不能为空！");
        }

    }



}
